from flask import Blueprint, request,send_file, send_from_directory
from app.response import ResponseBuilder
from app.services.FileService import FileService
import os

prefix = "/files"

file_bp = Blueprint("file", __name__, url_prefix=prefix)

@file_bp.route("/upload_sys_user_avatar", methods=["PUT"])
def upload_sys_user_avatar():
    file = request.files.get("file")
    file_service = FileService()
    file_path = file_service.save_file(file=file, path="upload/sys_user_avatar")
    return ResponseBuilder.success(data=prefix +"/"+ file_path, msg="上传成功")


@file_bp.route("/upload_soap_opera_cover", methods=["PUT"])
def upload_soap_opera_cover():
    file = request.files.get("file")
    file_service = FileService()
    file_path = file_service.save_file(file=file, path="upload/soap_opera_cover")
    return ResponseBuilder.success(data=prefix +"/"+ file_path, msg="上传成功")

@file_bp.route("/upload/<path:file_path>", methods=["GET"])
def get_file(file_path):
    file_service = FileService()
    file_path = os.path.join('upload/', file_path)
    return send_from_directory(file_service.basePath, file_path)

